//
//  YSGMyopiaRecordImageCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/1/5.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

protocol YSGMyopiaRecordImageCellDelegate: NSObjectProtocol {
    func recordImageCell(uploadImage cell: YSGMyopiaRecordImageCell)
    func recordImageCell(viewLargerImage cell: YSGMyopiaRecordImageCell)
}

class YSGMyopiaRecordImageCell: UITableViewCell {
    
    weak var delegate: YSGMyopiaRecordImageCellDelegate?
    
    var isEdited: Bool = false

    func configure(with vm: YSGMyopiaRecordDetailViewModel, at indexPath: IndexPath) {
        self.isEdited = vm.isEdited
        switch vm.model.checkItemCode {
        case .eyePhoto, .eyePrevious:
            uploadView.isHidden = true
            titleLabel.text = indexPath.row == 0 ? "左眼" : "右眼"
            let urlString = indexPath.row == 0 ? vm.model.oS ?? "" : vm.model.oD ?? ""
            imgView?.kf.setImage(with: URL(string: urlString))
            if urlString.isEmpty {
                titleLabel.text = ""
            }
                
        default:
            uploadView.isHidden = vm.model.url?.isEmpty == false
            titleLabel.text = "发布图像"
            imgView?.kf.setImage(with: URL(string: vm.model.url ?? ""))
        }
    }
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet weak var imgView: UIImageView! {
        didSet {
            imgView.contentMode = .scaleAspectFill
            imgView.backgroundColor = UIColor.textLight.withAlphaComponent(0.36)
            
            imgView.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(viewLargerImage))
            imgView.addGestureRecognizer(tap)
        }
    }
    
    @IBOutlet private weak var uploadView: UIView! {
        didSet {
            uploadView.dottedLineBorder(strokeColor: UIColor.textSupLight)
            
            let tap = UITapGestureRecognizer(target: self, action: #selector(uploadImage))
            uploadView.addGestureRecognizer(tap)
        }
    }
    
    @IBOutlet private weak var uploadLabel: UILabel! {
        didSet {
            let attriStrM = NSMutableAttributedString()
            let attriStr1 = NSAttributedString(
                string: "图片识别失败，",
                attributes: [
                    NSAttributedString.Key.foregroundColor: UIColor.textLight,
                    NSAttributedString.Key.font: UIFont.regular(12)
                ]
            )
            
            let attriStr2 = NSAttributedString(
                string: "手动上传",
                attributes: [
                    NSAttributedString.Key.foregroundColor:UIColor.mainColor,
                    NSAttributedString.Key.font: UIFont.regular(12)
                ]
            )
            
            attriStrM.append(attriStr1)
            attriStrM.append(attriStr2)
            
            uploadLabel.attributedText = attriStrM
        }
    }
}

extension YSGMyopiaRecordImageCell {
    @objc private func uploadImage() {
        if isEdited {
            HUDTool.show(message: "已经保存过检查，不能再上传图片")
            return
        }
        delegate?.recordImageCell(uploadImage: self)
    }
    
    @objc private func viewLargerImage() {
        guard imgView.image != nil else { return }
        
        if isEdited {
            delegate?.recordImageCell(viewLargerImage: self)
            return
        }
        delegate?.recordImageCell(uploadImage: self)
    }
}
